/*绑定回车到查询按钮*/
$(function(){
	$(document).keydown(function(){
		if(event.keyCode==13&&huichefs=='chaxun'){ 
			$("#chaxun").trigger('click'); 
			return false;
			}
	});
});
//数据读取
var mingcheng = '';
var leixing = '';
var jinyongbz = 0;
var pageNumber=1;
//定位参数
var dwid='';
var dingwei='';
var dwpanduan=true;
function exeShowPage() {
	var totalNumber = 0;
	var yibaoypbm = '';
    var sendData={
        "mingcheng":mingcheng,
        "leixing":leixing,
        "jinyongbz":jinyongbz,
        "yibaoypbm":yibaoypbm,
        "rukou":"1"
    };
    $.ajax({
        //请求方式为get
        type:"POST",
        //获取json文件路径
        url:"./zsyaopin/getCount",
        //传送数据
        data:sendData,
        async:false,
        //请求成功完成后要执行的方法
        success: function(data){
        	totalNumber = data;
	    	if(totalNumber==0){
				$("#a-table-list").css("display","none");
				$("#yp-img").html("<img src='"+path+"/image/chaxtj/cxtj-nobody.png'>");
			}else{
				$("#a-table-list").css("display","block");
				$("#yp-img").html("");
			}
        }
    });
    
    function getYaopin(name){
    	$('#kucunyp').html('');
    	var container = $('#pagination-'+name);
		container.pagination({
		dataSource:'./zsyaopin/getByPage?mingcheng='+encodeURI(mingcheng)+'&jinyongbz='+encodeURI(jinyongbz)+'&leixing='+encodeURI(leixing)+'&yibaoypbm='+yibaoypbm+'&rukou=1',
		locator: '',
		pageSize: 12,
		totalNumber: totalNumber,
		pageNumber:pageNumber,
		showNavigator: true,
   	    formatNavigator: '共 <%= totalNumber %> 条',
		callback: function(data, pagination){
			var content = '';
			$(data).each(function(index,value){
        	 if(this.jinyongbz==1 ){
        		 var zt="启用";
             }
             else if(this.jinyongbz==0 ){
            	 var zt="禁用";
             }
        	 if(zt=="启用" ){
        		 var szt="禁用";
             }
             else if(zt=="禁用"){
            	 var szt="启用";
             }
        	 if(this.dikucyjkg==1) {
         		var kcyj = "关闭预警";
         	}else {
         		var kcyj = "开启预警";
         	}
        	  if(this.chailingbz==0) {
        		var danwei = this.baozhuangdw;
        		var kucunsl = this.kucunsl+this.baozhuangdw;
        	}else{
        		var kucunsl = this.kucunsl+this.chailingdw;
        		var danwei = this.chailingdw;
        	}
        	  if(this.shengchancj==null) {
        		var shengchancj = "";
        	}else {
        		var shengchancj = this.shengchancj;
        	}
        	  if(this.leixing==1){
        		  var leixing="饮片";
        	  }else if(this.leixing==2){
        		  var leixing="中成药";
        	  }else if(this.leixing==3){
        		  var leixing="化学制药";
        	  }else if(this.leixing==4){
        		  var leixing="生物制药";
        	  }else if(this.leixing==5){
        		  var leixing="其他";
        	  }
				var mingcheng;
				if(this.bieming != null && this.bieming !='') {
					mingcheng = this.bieming;
				} else {
					mingcheng = this.mingcheng;
				}
			  var jinyongurl="./haocai/jinyongyg";
        	  var index=((pagination.pageNumber-1)*pagination.pageSize)+index+1;
        	content +='<tr id="ypjlhang'+this.id+'">'
			    	+'<td width="7%" class=col-sm-1  col-md-1 col-lg-1><label class=col-sm-12>'+index+'</label></td>'
			        +'<td width="19%" class="col-sm-1  col-md-1 col-lg-1 left">' +mingcheng+ "</td>" 
			        +'<td width="9%" class="col-sm-1  col-md-1 col-lg-1">' +leixing+"</td>" 
			        +'<td width="11%" class="col-sm-1  col-md-1 col-lg-1">' +this.baozhuanggg+"</td>" 
			        +'<td width="19%" class="col-sm-1  col-md-1 col-lg-1 left">' +shengchancj+"</td>" 
			       /* +'<td width="8%" class="col-sm-1  col-md-1 col-lg-1">' +this.jinhuojia.toFixed(2)+"</td>" */
			        +'<td width="9%" class="col-sm-1  col-md-1 col-lg-1" style="text-align: right;">' +this.xiaoshoujia.toFixed(8)+"</td>" 
			        +'<td width="7%" class="col-sm-1  col-md-1 col-lg-1">' +kucunsl+'</td>'
			        +'<td width="19%" class="col-sm-4 col-md-4 col-lg-3"><a class="operation picikgkz" id="tiaozhenkc" style="display:none;" data-toggle="modal" onclick="tiaozhengkucun('+this.id+','+this.kucunsl+',\''+danwei+'\','+1+',$(this))">调整库存</a>'
			        +'<a class="operation" id="yujingkq'+this.id+'"   data-toggle="modal" onclick="yujingkaiqi('+this.id+','+index+','+this.kucunxx+',\''+danwei+'\','+1+',$(this))">'+kcyj+'</a>'
			        +'<a class="operation" id="'+this.id+'" onclick="tiaozhuan('+this.id+','+pagination.pageNumber+')">详情</a>'
			        +'<a class="operation" id="qyjy'+this.id+'"   data-toggle="modal" onclick="jingyongqiyong('+pagination.pageNumber+','+this.id+','+index+','+1+',$(this))">'+zt+'</a>'
			        +'</tr>';
		        	if(dwid&&dwid==this.id){
		        		dingwei=this.id;
		        	}
			 });
			  $('#kucunyp').html(content);
				if(kucunpckg == true){
					$(".picikgkz").css("display","none");
				}else{
					$(".picikgkz").css("display","inline-block");
				}
			  if(dingwei!=null&&dingwei!=''){
				  $('#kucunyp').find('tr[id="ypjlhang'+dingwei+'"]').addClass('list-tr');
			  }
//			  if(tiaozhengkucun()==false){
//				  dwpanduan=false;
//			  }
			  if(!dwpanduan){
				  $('#kucunyp').find('tr[id="ypjlhang'+dingwei+'"]').removeClass('list-tr');
			  }
			}
		});
	 return container;
	}
	getYaopin('fenyyp');
	if(totalNumber <= 12) {
		$('#pagination-fenyyp').css('display','none');
	}else{
		$('#pagination-fenyyp').css('display','block');
	}
}
function getCanshu(predwid,predwpageNo,predwming,predwleixing,predwzhuangtai){
		dwid=predwid;
	if(dwid!='null'){
	//页面显示
		jinyongbz=predwzhuangtai;
		pageNumber=predwpageNo;
		leixing=predwleixing;
		mingcheng=decodeURI(decodeURI(predwming));
		$('#ypjl-mingcheng').val(mingcheng);
		$('#yp-leibie').val(leixing);
		$('#yp-zhuangt').val(jinyongbz);
		if(jinyongbz==0){
			$('#f-le-zhuangtai').text('启用');
		}else if(jinyongbz==2){
			$('#f-le-zhuangtai').text('全部');
		}else if(jinyongbz==1){
			$('#f-le-zhuangtai').text('禁用');
		}
		if(leixing==1){
			 $('#f-le-fenlei').text('饮片'); 
		  }else if(leixing==2){
			  $('#f-le-fenlei').text('中成药');
		  }else if(leixing==3){
			  $('#f-le-fenlei').text('化学制药');
		  }else if(leixing==4){
			   $('#f-le-fenlei').text('生物制药');
		  }else if(leixing==5){
			   $('#f-le-fenlei').text('其他');
		  }
	}
	exeShowPage();
}
//进入药品详情页面
function tiaozhuan(id,prepageNo){
	var dwpageNo=prepageNo;
	var dwming=$('#ypjl-mingcheng').val();
	var dwleixing=$('#yp-leibie').val();
	var dwzhuangtai=$('#yp-zhuangt').val();
	dwming=encodeURI(encodeURI(dwming));
	$("#cedan").load(path+"/views/kucun/yaopin-detail.jsp?ID="+id+"&dwming="+dwming+"&dwleixing="+dwleixing+"&dwzhuangtai="+dwzhuangtai+"&dwpageNo="+dwpageNo);
}

//查询按钮
$("#chaxun").click(function(){
	mingcheng = $('#ypjl-mingcheng').val();
	leixing = $('#yp-leibie').val();
	jinyongbz = $('#yp-zhuangt').val();
	pageNumber=1;
	dwpanduan=false;
    exeShowPage();
});
//select模拟
$(".dropdown-menu li").click(function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text(bText);
	$(this).parent().parent().find("#yp-zhuangt").val(bValue);
	$(this).parent().css("display","none");
	mingcheng = $('#ypjl-mingcheng').val();
	leixing = $('#yp-leibie').val();
	pageNumber=1;
	jinyongbz = bValue;
	dwpanduan=false;
    exeShowPage();
 });

//select模拟
$("#leixing li").click(function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text(bText);
	$(this).parent().parent().find("#yp-leibie").val(bValue);
	$(this).parent().css("display","none");
	mingcheng = $('#ypjl-mingcheng').val();
	leixing = bValue;
	pageNumber=1;
	dwpanduan=false;
	jinyongbz = $('#yp-zhuangt').val();
    exeShowPage();
 });
//查询条件重置按钮
$("#le-reset").click(function(){
	$('#ypjl-mingcheng').val('');
	$('#yp-leibie').val('');
	$('#yp-zhuangt').val(0);
	$('#f-le-fenlei').text('全部');
	$('#f-le-zhuangtai').text('启用');
	mingcheng = '';
	leixing = '';
	jinyongbz = 0;
	pageNumber=1;
	dwpanduan=false;
	exeShowPage();
});
//添加按钮
$("#le-add").click(function(){
	$(this).css("color","#ffffff");
	$("#cedan").load(path+"yaopin-add.jsp");
	return false; 
});

$("#tanceng-yptzkc .cancel").click(function(){
	 $("#add-subtraction").val("");
	 $("#tanceng-yptzkc").modal('hide');
});

$('#kuxunxx').keyup(function(){
	var val=$(this).val();
	var reg = /^[1-9]\d*$/ ;
	if(!reg.test(val)){
		notifyWarning("请输入正整数")
		$(this).val("");
	}
});
//判断库存批次是否开启
var kucunpckg;
function getYaopintjcs(prekucunpckg){
	kucunpckg = prekucunpckg;
}
//加.00
function getFloatStr(num){  
    if(num!=""){
    	num += ''; 
	    num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
	    if(/^0+/) //清除字符串开头的0
	        num = num.replace(/^0+/, '');
	    if(!/\./.test(num)) //为整数字符串在末尾添加.00
	        num += '.00000000';  
	    if(/^\./.test(num)) //字符以.开头时,在开头添加0
	        num = '0' + num;  
	    num += '00000000';        //在字符串末尾补零  
	    num = num.match(/\d+\.\d{8}/)[0];    
	    return num;
    }	
}; 
//处方价小数点后保存2位
$(document).on("focusout","[name='xiaoshoujia']",function(){
	var jine = $(this).val();
	   jine = getFloatStr(jine);
	   $(this).val(jine);
});